﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CSF.Utility.Mvc;
using CSF.Utility.Misc;
using CSF.Service.WebShop.Client;
using CSF.Products.Data;
using CSF.MultiLang.Data;
using CSF.Service.Accounts.Client;
using CSF.Security.Data;

namespace CSF.WebApp.Admin.Areas.Products.Controllers
{
    public class ProductCategoryController : SecureController
    {
        [NonAction]
        protected override void FillViewBag(AuthorizationContext filterContext)
        {
            base.FillViewBag(filterContext);

            ProductTypeSvcClient typesvc = new ProductTypeSvcClient();
            ViewBag.ProductTypes = typesvc.GetByAppId((int)ViewBag.AppId);
        }

        [CsfAuthorize]
        public ActionResult Index(bool isDisable = false)
        {
            ProductCategorySvcClient svc = new ProductCategorySvcClient();

            IEnumerable<ProductCategory> list = null;
            if (isDisable)
            {
                list = svc.GetDisable();
            }
            else
            {
                list = svc.GetEnable();
            }
            list = Search(list);

            ViewBag.IsDisable = isDisable;
            return View(list);
        }

        [NonAction]
        private IEnumerable<ProductCategory> Search(IEnumerable<ProductCategory> list)
        {
            list = list.Where(p => p.ApplicationId == ViewBag.AppId);

            var collection = Request.QueryString;

            var Code = collection["Code"];
            var name = collection["Name1"];

            if (!string.IsNullOrWhiteSpace(Code))
            {
                list = list.Where(p => Code.InStr(p.Code));
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                list = list.Where(p => name.InStr(new string[] { p.Name1/*, p.Name2, p.Name3*/ }));
            }
            return list;
        }

        [NonAction]
        private void PrepareViewData(int mlTextId = 0)
        {
            ProductCategorySvcClient catsvc = new ProductCategorySvcClient();
            ViewBag.ProductCategories = catsvc.GetEnable();

            if (mlTextId > 0)
            {
                LangTextContentSvcClient mlSvc = new LangTextContentSvcClient();
                ViewData["Name1Texts"] = mlSvc.GetByTextId(mlTextId);
            }
        }

        [CsfAuthorize]
        public ActionResult Add()
        {
            PrepareViewData();
            return View();
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [CsfAuthorize]
        [HttpPost]
        public ActionResult Add(ProductCategory model, IEnumerable<LangTextContent> Name1s)
        {
            if (ModelState.IsValid)
            {
                model.CreateBy = User.Identity.Name;

                // 保存多语言字符串
                LangTextDefSvcClient mlsvc = new LangTextDefSvcClient();
                bool mlsaved = mlsvc.SaveContents(model, Name1s);
                if (mlsaved)
                {
                    if (model.ParentId == 0)
                        model.ParentId = null;
                    ProductCategorySvcClient svc = new ProductCategorySvcClient();
                    model.Id = svc.Insert(model);
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Failed to save multi-languange texts.");
                }
            }
            PrepareViewData(model.NameTextId ?? 0);
            return View(model);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            ProductCategorySvcClient svc = new ProductCategorySvcClient();
            ProductCategory model = svc.GetOneById(id);

            PrepareViewData(model.NameTextId ?? 0);
            return View(model);

        }

        /// <summary>
        /// 编辑记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [CsfAuthorize]
        [HttpPost]
        public ActionResult Edit(ProductCategory model, IEnumerable<LangTextContent> Name1s)
        {
            if (ModelState.IsValid)
            {
                model.ModifyBy = User.Identity.Name;

                // 保存多语言字符串
                LangTextDefSvcClient mlsvc = new LangTextDefSvcClient();
                bool mlsaved = mlsvc.SaveContents(model, Name1s);
                if (mlsaved)
                {
                    if (model.ParentId == 0)
                        model.ParentId = null;
                    ProductCategorySvcClient svc = new ProductCategorySvcClient();
                    svc.UpdateById(model, model.Id);
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Failed to save multi-languange texts.");
                }
            }
            PrepareViewData(model.NameTextId ?? 0);
            return View(model);
        }

        /// <summary>
        /// 删除或禁用记录
        /// </summary>
        /// <param name="id">记录Id集合</param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult Delete(IEnumerable<int> id, string toUrl)
        {
            if (id != null && id.Count() > 0)
            {
                ProductCategorySvcClient svc = new ProductCategorySvcClient();
                svc.SetDisable(id, true, User.Identity.Name);
            }
            return Redirect(toUrl);
        }

        /// <summary>
        /// 删除或禁用记录
        /// </summary>
        /// <param name="id">记录Id集合</param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult Recover(IEnumerable<int> id, string toUrl)
        {
            if (id != null && id.Count() > 0)
            {
                ProductCategorySvcClient svc = new ProductCategorySvcClient();
                svc.SetDisable(id, false, User.Identity.Name);
            }
            return Redirect(toUrl);
        }
    }
}
